package com.qzsoft.system.common;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 取值工具类
 *
 * @author gbx
 * @since 2020-08-27 15:56:07
 */
public class ConvertUtils {
    private static Logger logger = LoggerFactory.getLogger(ConvertUtils.class);

    public ConvertUtils() {
    }

    public static <T> T sourceToTarget(Object source, Class<T> target) {
        if (source == null) {
            return null;
        } else {
            Object targetObject = null;

            try {
                targetObject = target.newInstance();
                BeanUtils.copyProperties(source, targetObject);
            } catch (Exception var4) {
                logger.error("convert error ", var4);
            }

            return (T)targetObject;
        }
    }

    public static <T> List<T> sourceToTarget(Collection<?> sourceList, Class<T> target) {
        if (sourceList == null) {
            return null;
        } else {
            List targetList = (List)sourceList.stream().map((t) -> {
                try {
                    T targetObject = target.newInstance();
                    BeanUtils.copyProperties(t, targetObject);
                    return targetObject;
                } catch (Exception var3) {
                    return null;
                }
            }).filter((t) -> {
                return t != null;
            }).collect(Collectors.toList());
            return targetList;
        }
    }
}
